package com.hp.gxw.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.constant.R;
import com.hp.gxw.entity.CostEquipmentPower;
import com.hp.gxw.service.ICostEquipmentPowerService;
import com.hp.gxw.service.ICostEquipmentService;
import com.hp.gxw.utils.ConditionUtil;
import com.hp.gxw.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-01-09
 */
@Controller
@RequestMapping("/manage/equipment_power")
public class CostEquipmentPowerController {

    @Autowired
    private ICostEquipmentPowerService iCostEquipmentPowerService;

    @Autowired
    private ICostEquipmentService iCostEquipmentService;

    @RequestMapping("/index")
    public String index(Model model){
        Map<String, Object> object = new HashMap<>();
        List<Map<String,Object>> cols = new ArrayList<>();
        Map<String,Object> checkboxMap = new HashMap<>();
        checkboxMap.put("type", "numbers");
        checkboxMap.put("title", "序号");
        cols.add(checkboxMap);
        Map<String,Object> map = new HashMap<>();
        map.put("field", "equipmentId");
        map.put("title", "设备ID");
        map.put("hide", true);
        map.put("minWidth", 150);
        Map<String,Object> map1 = new HashMap<>();
        map1.put("field", "equipmentName");
        map1.put("title", "设备");
        map1.put("minWidth", 150);
        cols.add(map1);
        List<Map<String,Object>> powers = new ArrayList<>();
        Map<String,Object> powerMap = null;
        CostEquipmentPower costEquipmentPower = iCostEquipmentPowerService.queryEquipmentPower("设备动燃");
        String[] split = costEquipmentPower.getElements().split(",");
        for(int i=0;i<split.length;i++) {
            powerMap = new HashMap<>();
            powerMap.put("field", split[i].split(":")[0]);
            powerMap.put("title", split[i].split(":")[1]);
            powerMap.put("minWidth", 100);
            powers.add(powerMap);
        }
        object.put("cols", cols);
        object.put("power", powers);
        model.addAttribute("obj",object);
        model.addAttribute("equipmentList",iCostEquipmentService.list());
        return "equipment_power/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public LayTableR list(String master) throws Exception {
        QueryWrapper<CostEquipmentPower> ew = ConditionUtil.convert(master, new QueryWrapper<>());
        List<CostEquipmentPower> list= iCostEquipmentPowerService.listInfo(ew);
        List<Map<String, Object>> result = new ArrayList<>();
        for (CostEquipmentPower equipmentPower : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("equipmentId", equipmentPower.getEquipmentId());
            map.put("equipmentName", equipmentPower.getEquipmentName());
            if(StringUtils.isNotEmpty(equipmentPower.getElements())){
                String[] split = equipmentPower.getElements().split(",");
                for(int i=0;i<split.length;i++) {
                    map.put(split[i].split(":")[0],split[i].split(":")[1]);
                }
            }
            result.add(map);
        }
        return LayTableR.result(0,result.size(),result);
    }

    @PostMapping("/change")
    @ResponseBody
    public R change(String obj, boolean type){
        try {
            CostEquipmentPower equipmentPower = JSON.parseObject(obj, CostEquipmentPower.class);
            if(type){
                CostEquipmentPower costEquipmentPower = iCostEquipmentPowerService.getOne(new LambdaQueryWrapper<CostEquipmentPower>()
                        .eq(CostEquipmentPower::getEquipmentId, equipmentPower.getEquipmentId())
                        .eq(CostEquipmentPower::getElementsId, equipmentPower.getElementsId()));
                if(costEquipmentPower == null){
                    equipmentPower.setCreateUser(ShiroUtil.getUserId());
                    equipmentPower.setCreateTime(new Date());
                    iCostEquipmentPowerService.save(equipmentPower);
                }
            }else{
                iCostEquipmentPowerService.remove(new LambdaQueryWrapper<CostEquipmentPower>()
                        .eq(CostEquipmentPower::getEquipmentId, equipmentPower.getEquipmentId())
                        .eq(CostEquipmentPower::getElementsId, equipmentPower.getElementsId()));
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }
}

